<?php
function translate_gb2big5($str) { 
	$g2b = new gb2big5();
	$str = $g2b->gb2big5($str);
	return $str;
}

class gb2big5 
{
	public $gb;
	
	function __construct()
	{
		$filename = dirname(__FILE__)."/gb-big5.table";
		$fp = fopen($filename, "r");
		$this->gb = fread($fp,filesize($filename));
		fclose($fp);
	}
	
	function gb2big5($Text)
	{
		$Text =str_replace('·','``',$Text);
		$Text =str_replace(' — ','^`',$Text);
		$Text =str_replace('–','^^',$Text);
		
			$Text = iconv("UTF-8", "GB2312//IGNORE",$Text);
		
		$max = strlen($Text)-1;
		for($i = 0; $i < $max; $i++) 
		{
			$h = ord($Text[$i]);
			if($h >= 160) 
			{
				$l = ord($Text[$i+1]);
				if($h==161 && $l==64) {
					$big = "　"; 
				}else{
					$p = ($h-160)*510+($l-1)*2;
					$big = $this->gb[$p].$this->gb[$p+1];
				}
				$Text[$i] = $big[0];
				$Text[$i+1] = $big[1];
				$i++;
			}	
		}
		
		$Text  = iconv("big5","utf-8",$Text);
		$Text =str_replace('``','·',$Text);
		$Text =str_replace('^`',' — ',$Text);
		$Text =str_replace('^^','–',$Text);
		return $Text; 
    
	}
}

?>